#! /usr/bin/env python3
# -*- encoding:utf-8 -*-
import helper.apiexception as apiex


class Page(object):

    # __slots__ = ("curPage", "pageSize", "totalPage", "totalSize", "resList")

    def __init__(self, curPage=1, pageSize=10):
        try:
            self.__curPage = int(curPage)
            if self.__curPage < 1:
                raise apiex.APIParamException(message="当前页数必须大于0")
            self.__pageSize = int(pageSize)
            if self.__pageSize < 1:
                raise apiex.APIParamException(message="每页条数必须大于0")
        except apiex.APIException as ae:
            raise ae
        except Exception as e:
            raise ValueError("参数错误")

    @property
    def curPage(self):
        return self.__curPage

    @curPage.setter
    def curPage(self, curPage):
        if not isinstance(curPage, int):
            raise AttributeError("当前页数必须为数字")
        if curPage <= 0:
            raise AttributeError("当前页数必须大于0")
        self.__curPage = curPage

    @property
    def pageSize(self):
        return self.__pageSize

    @pageSize.setter
    def pageSize(self, pageSize):
        if not isinstance(pageSize, int):
            raise AttributeError("每页条数必须为数字")
        if pageSize <= 0:
            raise AttributeError("每页条数必须大于0")
        self.__pageSize = pageSize

    @property
    def totalPage(self):
        return self.__totalPage

    @property
    def totalSize(self):
        return self.__totalSize

    @property
    def curSize(self):
        return self.__curSize

    @property
    def resList(self):
        return self.__resList

    def trans2Page(self, totalSize, resList):
        if not isinstance(totalSize, int):
            raise AttributeError("总条数必须为数字")
        self.__totalSize = totalSize
        self.__totalPage = self.__totalSize // self.__pageSize
        if resList is not None and len(resList) > 0:
            if not isinstance(resList, list):
                raise AttributeError("数据必须为list类型")
            self.__resList = resList
            self.__curSize = len(resList)
        else:
            self.__resList = {}
            self.__curSize = 0
